package com.carpedil.order.service.impl;

import com.carpedil.order.entity.Order;
import com.carpedil.order.mapper.OrderMapper;
import com.carpedil.order.service.OrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/** @author Administrator */
@Service
public class OrderServiceImpl implements OrderService {
  @Autowired RabbitTemplate rabbitTemplate;
  @Autowired RestTemplate restTemplate;
  @Autowired OrderMapper orderMapper;

  @Override
  public String payment(String orderId, String movieName) {
    orderMapper.saveOrder(new Order().setOrderId(orderId).setMovieName(movieName).setStatus(0));
    try {
      Thread.sleep(5000);
      rabbitTemplate.convertAndSend(
          "orderExchange", "j178.order", "订单号:" + orderId + " , 电影名:" + movieName);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return "订票成功,等待出票中,订单号:" + orderId;
  }

  @Override
  public String finish(String movieName) {
    int i = orderMapper.updateOrderStatus(movieName);
    return i > 0 ? "出票成功" : "出票失败";
  }

  @Override
  public String check(String orderId) {
    Order order = orderMapper.checkById(orderId);
    return order.getStatus() == 1 ? "出票成功" : "正在出票中,请稍后";
  }
}
